package org.jing.ext.pdf.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import org.jing.core.lang.JingException;
import org.jing.core.util.GenericUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * Description: <br>
 *
 * @author bks <br>
 * @since 2021-06-23 <br>
 */
public class PdfOperator {

    public static void combinePdfByFileList(List<File> fileList, String outputFile) {
        combinePdfByFileList(fileList, new File(outputFile));
    }

    public static void combinePdfByFileList(List<File> fileList, File outputFile) {
        int size = GenericUtil.count(fileList);
        if (0 == size) return;
        Document document = null;
        PdfReader reader;
        try {
            document = new Document((reader = new PdfReader(fileList.get(0).getAbsolutePath())).getPageSize(1));
            reader.close();
            FileOutputStream writer = new FileOutputStream(outputFile);
            PdfCopy copy = new PdfCopy(document, writer);
            document.open();
            int pageCount;
            PdfImportedPage page;
            for (int i$ = 0; i$ < size; i$++) {
                reader = new PdfReader(fileList.get(i$).getAbsolutePath());
                pageCount = reader.getNumberOfPages();
                for (int j$ = 1; j$ <= pageCount; j$++) {
                    document.newPage();
                    page = copy.getImportedPage(reader, j$);
                    copy.addPage(page);
                }
                reader.close();
            }
        }
        catch (JingException e) {
            throw e;
        }
        catch (Throwable t) {
            throw new JingException(t);
        }
        finally {
            if (null != document && document.isOpen()) {
                document.close();
            }
        }
    }

    public static void combinePdfByFilePathList(List<String> fileList, String outputFile) {
        combinePdfByFilePathList(fileList, new File(outputFile));
    }

    public static void combinePdfByFilePathList(List<String> fileList, File outputFile) {
        int size = GenericUtil.count(fileList);
        if (0 == size) return;
        Document document = null;
        PdfReader reader;
        try {
            document = new Document((reader = new PdfReader(fileList.get(0))).getPageSize(1));
            reader.close();
            FileOutputStream writer = new FileOutputStream(outputFile);
            PdfCopy copy = new PdfCopy(document, writer);
            document.open();
            int pageCount;
            PdfImportedPage page;
            for (int i$ = 0; i$ < size; i$++) {
                reader = new PdfReader(fileList.get(i$));
                pageCount = reader.getNumberOfPages();
                for (int j$ = 1; j$ <= pageCount; j$++) {
                    document.newPage();
                    page = copy.getImportedPage(reader, j$);
                    copy.addPage(page);
                }
                reader.close();
            }
        }
        catch (JingException e) {
            throw e;
        }
        catch (Throwable t) {
            throw new JingException(t);
        }
        finally {
            if (null != document && document.isOpen()) {
                document.close();
            }
        }
    }
}
